package com.zcx.qshop.db;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.zcx.helper.db.SQLiteTable;
import com.zcx.helper.db.note.SQLiteTableInfo;
import com.zcx.qshop.entity.Good;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2015/9/2 0002.
 */
@SQLiteTableInfo(name = "history", structure = { "code", "pid", "picurl", "title", "description", "number", "price", "unit" } )
public class HistoryTable extends SQLiteTable {

    private SQLiteDatabase db;

    public void insert(String code, Good good){

        if(good.picurl != null && good.title != null && good.price != null && good.unit != null &&!isHas(good)){

            db = dataBase.getWritableDatabase();

            ContentValues contentValues = new ContentValues();

            contentValues.put("code", code);

            contentValues.put("pid", good.pid);

            contentValues.put("unit", good.unit);

            contentValues.put("picurl", good.picurl);

            contentValues.put("title", good.title);

            contentValues.put("description", good.description);

            contentValues.put("number", good.number);

            contentValues.put("price", good.price);

            db.insert("history", "_id", contentValues);

            db.close();

        }

    }

    public boolean isHas(Good good){

        db = dataBase.getReadableDatabase();

        Cursor cursor = db.query("history", new String[]{"pid", "picurl", "title", "description", "number", "price", "unit"}, "pid=?", new String[]{ good.pid }, null, null, null);

        int count = cursor.getCount();

        cursor.close();

        db.close();

        return count > 0;

    }

    public void delete(Good good){

        db = dataBase.getWritableDatabase();

        db.delete("history", "pid=?", new String[]{good.pid});

        db.close();

    }

    public void deleteAll(){

        db = dataBase.getWritableDatabase();

        db.delete("history", null, null);

        db.close();

    }

    public List<Good> queryAll(String code){

        List<Good> list = new ArrayList<>();

        db = dataBase.getReadableDatabase();

        Cursor cursor = db.query("history", new String[]{"pid", "picurl", "title", "description", "number", "price", "unit"}, "code=?", new String[]{ code }, null, null, "_id desc");

        while (cursor.moveToNext()) {

            Good good  = new Good();

            good.pid = cursor.getString(cursor.getColumnIndex("pid"));

            good.picurl = cursor.getString(cursor.getColumnIndex("picurl"));

            good.title = cursor.getString(cursor.getColumnIndex("title"));

            good.description = cursor.getString(cursor.getColumnIndex("description"));

            good.number = cursor.getString(cursor.getColumnIndex("number"));

            good.price = cursor.getString(cursor.getColumnIndex("price"));

            good.unit = cursor.getString(cursor.getColumnIndex("unit"));

            list.add(good);

        }

        cursor.close();

        db.close();

        return list;

    }

}
